/**
 * This class is used to keep track of the addresses of the patients
 */
import java.sql.ResultSet;

/**
 * A Struct-like class for holding an address
 */
public class Address {
	private String street;
	private String city;
	private String state;
	private int zipCode;

	/**
	 * Creates a new instance of Address
	 * 
	 * @param street
	 *            the patient's street address
	 * @param city
	 *            the patient's city
	 * @param state
	 *            the patient's state
	 * @param zip
	 *            the patient's zip code
	 */
	public Address(String street, String city, String state, int zip) {
		this.street = street;
		this.city = city;
		this.state = state;
		this.zipCode = zip;
	}

	/** Creates a new instance of Address */
	public Address() {
		this(null, null, null, 0);
	}

	/**
	 * Returns the patient's street address @return the patient's street address
	 */
	public String getStreet() {
		return street;
	}

	/**
	 * Sets the patient's street address to the specified value @param street
	 * the specified value
	 */
	public void setStreet(String street) {
		this.street = street;
	}

	/** Returns the patient's city @return the patient's city */
	public String getCity() {
		return city;
	}

	/**
	 * Sets the patient's city to the specified value @param city the specified
	 * value
	 */
	public void setCity(String city) {
		this.city = city;
	}

	/** Returns the patient's state @return the patient's state */
	public String getState() {
		return state;
	}

	/**
	 * Sets the patient's state to the specified value @param state the
	 * specified value
	 */
	public void setState(String state) {
		this.state = state;
	}

	/** Returns the patient's zip code @return the patient's zip code */
	public int getZipCode() {
		return zipCode;
	}

	/**
	 * Sets the patient's zip code to the specified value @param zipCode the
	 * specified value
	 */
	public void setZipCode(int zipCode) {
		this.zipCode = zipCode;
	}

	// CRUD functionality
	// Create
	public static boolean createAddress(String username, String street,
			String city, String state, int zip) {
		String sql = "INSERT INTO Address VALUES (username='" + username
				+ "', street='" + street + "', city='" + city + "', state='"
				+ state + "', zip_code=" + zip + ")";
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// Read
	public static Address readAddress(String username) {
		String foundStreet = "";
		String foundCity = "";
		String foundState = "";
		int foundZip = 0;
		String sql = "SELECT * FROM Address WHERE username='" + username + "'";
		ResultSet rs = Database.selectQuery(sql);
		// should only be 1 address per username
		Address foundAddress;
		try {
			while (rs.next()) {
				foundStreet = rs.getString("street");
				foundCity = rs.getString("city");
				foundState = rs.getString("state");
				foundZip = rs.getInt("zip_code");
			}
		} catch (Exception e) {
		}
		foundAddress = new Address(foundStreet, foundCity, foundState, foundZip);
		return foundAddress;
	}

	// Update
	public static boolean updateAddress(String username, String street,
			String city, String state, int zip) {
		String sql = "UPDATE Address SET street='" + street + "', city='"
				+ city + "', state='" + state + "', zip_code=" + zip
				+ " WHERE username='" + username;
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// Delete
	public static boolean deleteAddress(String username) {
		String sql = "DELETE FROM Address WHERE username='" + username + "'";
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
